# -*- coding:utf-8 -*-
# @Time : 2021/2/24 10:17
# @Author : Administrator
# @File : bibibi.py
# @Software: PyCharm
# @Motto: good good study,day day up


import requests
import re


def get_response(url):
    headers = {
        'cookie': 'rpdid=kwmpkqqspodospsxkpwqw; fts=1545641635; LIVE_BUVID=AUTO7115561846511675; PVID=1; _uuid=C5312D9B-7990-1AF7-829F-149ADA49E47035233infoc; blackside_state=1; CURRENT_FNVAL=80; CURRENT_QUALITY=64; buvid3=0D1B1A57-E8AF-469D-B613-AE029F929853143105infoc; bsource=share_source_qqchat; fingerprint=a3492744d803b4f66b2697be7e85f05e; buvid_fp=0D1B1A57-E8AF-469D-B613-AE029F929853143105infoc; buvid_fp_plain=0D1B1A57-E8AF-469D-B613-AE029F929853143105infoc; SESSDATA=a96a4f67%2C1629684755%2Ca6e2e%2A21; bili_jct=3a3048dc5ddf6090ef429a91580e5135; DedeUserID=82397762; DedeUserID__ckMd5=21b2b3fcec793a40; sid=7mymf36u; bfe_id=61a513175dc1ae8854a560f6b82b37af',
        'origin': 'https://www.bilibili.com',
        'referer': 'https://www.bilibili.com/',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    return response


def get_data(url):
    response = get_response(url)
    json_data = response.json()
    data = json_data["data"]
    return data


def save(content):
    for i in content:
        with open('B站弹幕.txt', 'a', encoding='utf-8') as f:
            f.write(i)
            f.write('\n')
            print(i)


def main(url):
    data = get_data(url)
    for date in data:
        url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=120004475&date={date}'
        html_data = get_response(url).text
        result = re.findall(".*?([\u4E00-\u9FA5]+).*?", html_data)
        save(result)


if __name__ == '__main__':
    one_url = 'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-02'
    main(one_url)